<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\UserAskRequest;

class UserAskController extends Controller
{
   
    public function getIndex()
    {
        // 初始化操作
         //初始化操作

      //获取全部以及分类
       $type = DB::table('type')->where('pid',0)->get();
      // dd($type);

      //获取配置信息
       $config = DB::table('config')->first();

      //友情链接
       $blinks = DB::table('blink')->get();

      //遍历下部的栏目
      $datas = self::pushCates(self::pushType(),0);

      //查询所有的申请类型
      $cates = DB::table('ask')->get();

      // var_dump($type);
       return view('Home.loveser.userask',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas,'cates'=>$cates]);
    }

    //申请救助信息
    public function getAdd($id)
    {


    //获取全部以及分类
       $type = DB::table('type')->where('pid',0)->get();
    // dd($type);

    //获取配置信息
       $config = DB::table('config')->first();

    //友情链接
     $blinks = DB::table('blink')->get();

    //遍历下部的栏目
    $datas = self::pushCates(self::pushType(),0);

    //查询申请的类别信息
     $mess = DB::table('ask')->where('id',$id)->first();

      // var_dump($type);
       return view('Home.loveser.askmess',['type'=>$type,'config'=>$config,'blinks'=>$blinks,'datas'=>$datas,'mess'=>$mess]);
    }

     //获取全部一级分类板块
    public static function pushType()
    {
        return $cates = DB::table('type')->get();
    }

    
    //获取二级分类
    public  static function pushCates($cates,$pid)
    {
        $data = [];
        foreach($cates as $k=>$v)
        {
            if($v->pid == $pid){

               $v->sub = self::pushCates($cates,$v->id);
               $data[] = $v;
            }
        }
        return $data;
    }


   //申请信息添加
    public function postInsert(UserAskRequest $request)
    {
        // dd($request->all());
        //提取数据
        $data = $request->except('_token');
        
        $data['time']= time();
        $data['status']=0;

        //入库操作
        $res = DB::table('askuser')->insert($data);
        // dd($res);

        if($res){

            return redirect('/homes/ask')->with('success','申请信息提交成功,会尽快联系您');
        }else{

            return back()->with('error','信息提交失败');
        }
    }
}
